package com.bookpavilion.chat.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WSServer {

    private EventLoopGroup masterGroup;
    private EventLoopGroup slaveGroup;
    private ServerBootstrap serverBootstrap;
    private ChannelFuture future;
    private static class SingletionWSServer{
        private static final WSServer instance = new WSServer();
    }

    public static WSServer getInstance(){
        return SingletionWSServer.instance;
    }

    public WSServer(){
        masterGroup=new NioEventLoopGroup();
        slaveGroup = new NioEventLoopGroup();
        serverBootstrap = new ServerBootstrap();
        serverBootstrap.group(masterGroup,slaveGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new WSServerInitializer());

    }
    public void start(){
        future=serverBootstrap.bind(8088);
        System.err.println("[netty websocket server] 启动完毕");
    }
}
